<?php 
class ExpsController extends AppController {

	var $name = 'Exps';
	
	var $uses = array('Result','Location','Friendship','Checkin',
        'Totalcheckin','Test','User');
	var $helpers = array('Html', 'Form',  'Ajax','flashChart');
	var $components = array('Inf');
	
    function test(){
        $this->User->recursive=-1;
        $users = $this->User->find('list');
        $this->Location->recursive=-1;
		$locations = $this->Location->find('list');
     
        $influenced_users=array();
       	if (!empty($this->data)) {
   	        $influenced_users=$this->Checkin->find('list',
                array(
                    'fields'=>array('user_id','user_id'),
                    'conditions'=>
                        array('location_id'=>$this->data['Exp']['location_id'],'user_id'=>$this->data['Exp']['user_id'])));
            print_r($influenced_users);
        }
        $this->set(compact('users', 'locations','influenced_users'));
    }
    function result(){
        $influenced_users=array();
       	if (!empty($this->data)) {
       	    $t1=$this->data['Exp']['t1']['year']."/".
                $this->data['Exp']['t1']['month']."/".
                $this->data['Exp']['t1']['day'];
            $t2=$this->data['Exp']['t2']['year']."/".
                $this->data['Exp']['t2']['month']."/".
                $this->data['Exp']['t2']['day'];
   	        $location=$this->data['Exp']['location_id'];
            $user=$this->data['Exp']['user_id'];
            
   	        $time=$this->Checkin->query("
                select min(time) as t 
                from checkins
                where user_id=".$user."
                    and location_id='".$location."'
                    and time between '".$t1."' 
                        and '".$t2."'");
            $friends=$this->Friendship->find('list',
                    array(
                        'fields'=>array('withuser'),
                        'conditions'=>array('user_id'=>$user)
            ));
            $influenced_users=$this->Checkin->find('list',
                    array(
                        'fields'=>array('user_id','user_id'),
                        'conditions'=>array(
                            'user_id'=>$friends,'time BETWEEN ? AND ?' => 
                                array($time[0][0]['t'],$t2))));
            print_r($time);
            print_r($this->data['Exp']['t2']);
            print_r($influenced_users);
        }
        $this->set(compact('influenced_users','friends','t1','t2','location','user'));
    }
    
     /**
     * this function used for testing influential users at a Location
     * @param 
     * @param
     * 
     * @return link to the result
     * */
    function testl(){
    
        $this->Location->recursive=-1;
		$locations = $this->Totalcheckin->find('all',array(
            'fields'=>array('location_id','total')
           ));
        $this->set(compact('locations'));
     
     }
    /**
     * this function used for testing influential users at a Location SQL
     * @param 
     * @param
     * 
     * @return a set of influential users
     * */
      function resultl(){
        $local=$this->data['Exp']['location_id'];
        $t1=$this->data['Exp']['t1']['year']."/".
                $this->data['Exp']['t1']['month']."/".
                $this->data['Exp']['t1']['day'];
        $t2=$this->data['Exp']['t2']['year']."/".
                $this->data['Exp']['t2']['month']."/".
                $this->data['Exp']['t2']['day'];
        
                
        $users=$this->Checkin->find('list',array(
            'fields'=>array('user_id','user_id'),
            'conditions'=>array(
                'location_id'=>$local),
                'time BETWEEN ? AND ?' =>array($t1,$t2)));
        
        
        $id=$this->Test->find('first',array('fields'=>array('id'),'conditions'=>array(
            'location_id'=>$local,
            't1'=>$t1,
            't2'=>$t2)));
        $id=$id['Test']['id'];
       	if(empty($id)){
            $this->Test->create();
            $this->data['Test']['location_id']=$local;
            $this->data['Test']['t1']=$t1;
            $this->data['Test']['t2']=$t2;
            if (!$this->Test->save($this->data)) {
			     $this->Session->setFlash(__('The experiment could not be saved', true));
                 $this->redirect(array('action'=>'testl'));
            }
            $this->Session->setFlash(__('Data saved',true));
            $id=$this->Test->id;
        
            foreach ($users as $u){
                ini_set('max_execution_time', 0);
                $this->Inf->inflence_user_location_sql($local,$u,$t1,$t2,$id);
            }
        }
        $this->redirect(array('action'=>'r1',$id));
     //   $this->Result->recursive=-1;
      //  $r=$this->Result->find('all',array('conditions'=>array('test_id'=>$id)));
        //$this->set('r',$this->paginate(array('conditions'=>array('test_id'=>$id))));
        
        //$this->result->recursive=-1;
    //    $this->set('r',$this->paginate(array('test_id'=>$id)));
    }
    
    var $paginate=array('limit'=>100);
    function r1($id){
        $u1=$this->Result->find('list',array('fields'=>array('u1','u1'),'conditions'=>array('test_id'=>$id)));
        $u2=$this->Result->find('list',array('fields'=>array('u2','u2'),'conditions'=>array('test_id'=>$id)));
        $this->set('r',$this->paginate(array('test_id'=>$id)));
        $this->set(compact('u1','u2'));
    }
    
    function infLocation($id){
        
    }
    //function resultl(){
//        $local=$this->data['Exp']['location_id'];
//        $t1=$this->data['Exp']['t1']['year']."/".
//                $this->data['Exp']['t1']['month']."/".
//                $this->data['Exp']['t1']['day'];
//        $t2=$this->data['Exp']['t2']['year']."/".
//                $this->data['Exp']['t2']['month']."/".
//                $this->data['Exp']['t2']['day'];
//        $temp=array();
//                
//        $users=$this->Checkin->find('list',array(
//            'fields'=>array('user_id','user_id'),
//            'conditions'=>array(
//                'location_id'=>$local),
//                'time BETWEEN ? AND ?' =>array($t1,$t2)));
//        $sum=array();
//        foreach ($users as $u){
//            $u1=array($u);
//            //tap hop gia tri influenced users
//            $s=array($u=>$this->Inf->inflence_user_location($local,$u,$t1,$t2));
//          //  $s[$u]=$this->Inf->inflence_user_location($local,$u,$t1,$t2);
//            
//            $sum=array_merge($sum,$s);
//          //  $temp=$this->Math->doComplexOperation(2,3);
//            print_r($s);
//            print_r($sum);
//        }
//        
 //   }<br />
      /**
     * Ham xu ly theo MANG
     * @param 
     * @param
     * 
     * @return link to the result
     * */
    function testl1(){
    
        $this->Location->recursive=-1;
		$locations = $this->Totalcheckin->find('all',array(
            'fields'=>array('location_id','total')
           ));
        $this->set(compact('locations'));
     
     }
     /** Ham xu ly theo MANG
     * @param 
     * @param
     * 
     * @return link to the result
     * */
   
     function resultl1(){
        $local=$this->data['Exp']['location_id'];
        $t1=$this->data['Exp']['t1']['year']."/".
                $this->data['Exp']['t1']['month']."/".
                $this->data['Exp']['t1']['day'];
        $t2=$this->data['Exp']['t2']['year']."/".
                $this->data['Exp']['t2']['month']."/".
                $this->data['Exp']['t2']['day'];
        $temp=array();
                
        $users=$this->Checkin->find('list',array(
            'fields'=>array('user_id','user_id'),
            'conditions'=>array(
                'location_id'=>$local),
                'time BETWEEN ? AND ?' =>array($t1,$t2)));
        $sum=array();
        foreach ($users as $u){
            $u1=array($u);
        //    tap hop gia tri influenced users
            $s=array($u=>$this->Inf->inflence_user_location($local,$u,$t1,$t2));
          //  $s[$u]=$this->Inf->inflence_user_location($local,$u,$t1,$t2);
            
         //   $sum=array_merge($sum,$s);
          //  $temp=$this->Math->doComplexOperation(2,3);
            print_r($s);
          //  print_r($sum);
        }
        
    }
    function update_select() {
      
     	$lopths = $this->Location->find('list',array('limit'=>10));
        $this->set(compact('lopths'));
    }
    
    function maxInf($users){
        $r=array();//include k influential users
        $u1=array();//the influence spread of k influtial users
        $u2=array();//includes users are not considered
        
        $u2=$users;
        
        array_diff_assoc();
    }
   
    
/*
    function update_selectp() {
        if(!empty($this->data['Chart']['hocky_id'])) 
            $cat_id = (int)$this->data['Chart']['hocky_id'];
        else
            $cat_id =$this->Session->read('hknk') ;
            
        $lopths=$this->Lopth->query('
                    select Lopth.id, Monhoc.tenmon, Monhoc.mamon from 
                        lopths as Lopth, monhocs as Monhoc 
                    where Lopth.monhoc_id=Monhoc.id
                    and hknk_id='.$cat_id);
        $this->set(compact('lopths'));
    }
    
 
  
  */
}
?>